var express = require('express')
var app = express()
var fs = require('fs')
var multer  = require('multer')

var bodyParser = require('body-parser')
const { memoryStorage } = require('multer')
// app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())

app.set('view engine', 'ejs');

// 创建目录，上传文件
var createFolder = function (folder) {
    try {
        fs.accessSync(folder);
    } catch (e) {
        fs.mkdirSync(folder);
    }
};

var uploadFolder = './upload/';

createFolder(uploadFolder);

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, uploadFolder);
    },
    filename: function (req, file, cb) {
        cb(null, file.originalname);
    }
});
var upload = multer({ storage: storage });

// app.get('/form', function (req, res) {
//     // var form = fs.readFileSync('./form.html', { encoding: "utf8" })
//     res.sendfile(__dirname + './form.html')
// })

// app.get('/form/:name', function (req, res) {
//     var person = req.params.name;
//     res.render('form', {person: person})//前面的person是form.ejs定义的，后面的person是var person
// })

app.get('/form/:name', function (req, res) {
    var data = {name: 'Morty', age: 24, hobbies: ['sleeping', 'gaming', 'hiking'] };
    res.render('form', {data: data})//前面的person是form.ejs定义的，后面的person是var person
})

app.get('/about', function (req, res) {
    res.render('about');
})

app.post('/upload', upload.single('logo'), function (req, res) {
    console.dir(req.file); // 列出文件的所有属性
    res.send({ 'ret_code': 0 })
})

app.post('/multipleDownloadAction_test', function (req, res, next) {
    var downloadData = {
        "result": true,
        "info": "Success!",
        "data": {
            "jobId": "202103290246258jin3c"
        }
    }
    res.send(downloadData);
});

var count = 0;
app.get('/pollDownloadResult_tes', function (req, res, next) {
    console.log("----: ", req.query, ++count);
    if(count == 3) {
        var data = {
            "result": true,
            "info": "Finished",
            "data": {
                "download_url": "http://localhost:3100/download_test?file=666.gif"
            }
        }
        res.send(data);
    } else {
        var data = {
            "result": true,
            "info": "Running",
            "data": null
        }
        res.send(data);
    }
});

app.get('/download_test', function (req, res, next) {
    count = 0;
    res.download("./public/test.txt", err => {
    //res.download("./public/" + "R&M.avi", err => {
        if (err) {
            res.send("下载失败！");
        } else {
            console.log("下载成功！");
        }
    });
});

app.get('/', function (req, res) {
    console.dir(req.query);
    res.send("home page: " + req.query.find);
})

app.post('/', function(req, res) {
    console.dir(req.body);
    res.send('OK')
})

app.get('/home/:id', function (req, res) {
    console.dir(req.params);
    res.send("responseObject " + req.params.id);
})

app.listen(3100)

console.log("listening to port 3100")